<?php
namespace app\api\service;
use think\Controller;
use think\Loader;

class Nim extends Controller {

    // 获取云信账户信息
    static public function get_nim_account($param) {
        if( !isset($param['type']) ||  !isset($param['member_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $member_types = ['member', 'repair', 'dealer', 'partner'];
            $member_type = $member_types[$param['type']];

            $where = [
                'member_id' => $param['member_id'],
                'member_type' => $member_type
            ];
            $nim_account = db('nim_account')->field('accid, token')->where($where)->find();
            if (!$nim_account) {
                // 注册云信ID
                $data = self::create_action($param);
                if ($data['code'] == 200) {
                    $nim_account = $data['data'];
                } else {
                    return $data;
                }
            }
            return ['data'=>$nim_account,'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['data'=>'','code'=>0,'msg'=> $e->getMessage()];
        }
    }

    // 注册云信ID
    static public function create_action($param) {
        $member_types = ['member', 'repair', 'dealer', 'partner'];
        $member_type = $member_types[$param['type']];

        //获取用户基础信息
        $member_role_info = db('member_role_info')->field('nickname, headimgurl')->where('type', $param['type'])->where('member_id', $param['member_id'])->find();

        $accid = time() . $param['member_id'];
        $name = $member_role_info['nickname'] ? $member_role_info['nickname'] : '匿名';
        $str = 'accid=' . $accid . '&name=' . $name;
//            '&icon=' . strstr($member_role_info['headimgurl'], 'http://') ? $member_role_info['headimgurl'] : 'http://' . $_SERVER['HTTP_HOST'] . $member_role_info['headimgurl'];

        $url = 'https://api.netease.im/nimserver/user/create.action';
        $headers = array();
        $time = time();
        $AppKey = '49375d56619322b7f929cc3c13c62195';
        $AppSecret  = '3980db0ae0d5';
        $Nonce = md5(date('YmdHis')).rand(10000, 99999);
        $CheckSum = sha1($AppSecret . $Nonce . $time);
        array_push($headers, "AppKey:$AppKey");
        array_push($headers, "Nonce:$Nonce");
        array_push($headers, "CurTime:$time");
        array_push($headers, "CheckSum:$CheckSum");

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_HEADER, true);

        curl_setopt ( $curl, CURLOPT_URL, $url);
        curl_setopt ( $curl, CURLOPT_POST, 1 );
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $str);

        if (1 == strpos("$".$url, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $rs =  curl_exec($curl);

        $rs = substr($rs, strpos($rs, '{'));
        $rs = json_decode($rs, true);
        if ($rs['code'] == 200) {
            //发送成功 写入数据库
            $info = $rs['info'];
            $info['member_id'] = $param['member_id'];
            $info['member_type'] = $member_type;
            $rs = db('nim_account')->insertGetId($info);
            if ($rs) {
                return ['code' => 200, 'data' => $info, 'msg' => 'SUCCESS'];
            } else {
                return $rs;
            }
        } else {
            return $rs;
        }
    }

}





